﻿using Autofac.Annotation;
using AutoMapper;
using Micro.Extensions.DDD.Bus;
using Module.Test.Application.Dtos;
using Module.Test.Application.IServices;
using Module.Test.Domain.Commands.Customs;
using Module.Test.Domain.IRepositorys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Module.Test.Application.Services
{
    //[Component(AutofacScope =AutofacScope.InstancePerLifetimeScope)]
    public class CustomAppService : ICustomAppService
    {
        // 仓储
        private readonly ICustomRepository _customRepository;
        // 用来进行DTO
        private readonly IMapper _mapper;
        // 中介者 总线
        private readonly IMediatorHandler _bus;

        public CustomAppService(IMapper mapper, IMediatorHandler bus, ICustomRepository customRepository)
        {
            this._mapper = mapper;
            this._bus = bus;
            this._customRepository = customRepository;
        }

        public async Task<IList<CustomDto>> GetAll()
        {
            var list = await _customRepository.Query();
            return _mapper.Map<IList<CustomDto>>(list);

        }

        public async Task<CustomDto> GetById(long id)
        {
            var info = await _customRepository.QueryById(id);
            if (info == null)
                return null;
            return _mapper.Map<CustomDto>(info);
        }

        public async Task<bool> Register(CustomDto customDto)
        {
            var registerCommand = _mapper.Map<RegisterCustomCommand>(customDto);
            var result = await _bus.SendCommand(registerCommand);
            return result;
        }

        public async Task<bool> Update(CustomDto customDto)
        {
            var updateCommand = _mapper.Map<UpdateCustomCommand>(customDto);
            var result = await _bus.SendCommand(updateCommand);
            return result;
        }

        public async Task<bool> Remove(long id)
        {
            var removeCommand = new RemoveCustomCommand(id);
            var result = await _bus.SendCommand(removeCommand);
            return result;
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}
